<?php

use framework\libraries\Controller;
use framework\support\Db;
use framework\support\Redis;
use framework\libraries\Utils;
use framework\support\Model;

class admin extends Controller
{

    function projects_get($id = null)
    {
        if ($id) {
            $res = Model::getItemByID("project", $id);
        } else {
            $res = Db::select("project", "*");
        }

        return $this->success($res);
    }


    function code_get()
    {
        $code = md5(Utils::randomString('alnum', 32));
        $expire = $this->get("days");
        if ($expire) {
            $date = date("Y-m-d H:i:s", strtotime("+{$expire} days"));
        } else {
            $expire = $this->get("hours");
            if ($expire) {
                $date = date("Y-m-d H:i:s", strtotime("+{$expire} hours"));
            }
        }
        $data = [
            'code' => $code,
            'expire_time' => $date,
        ];
        $id = Db::insert("code", $data);
        if ($id) {
            $this->success($code);
        } else {
            $this->error(40401, "生成错误");
        }
    }

    function use_total_get()
    {
        $count = Db::count("code", [
            "used_time[>]" => '2024-10-01',
        ]);
        return $this->success($count);
    }
}
